package jam.UserInterface.UI;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import javax.imageio.ImageIO;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.UIManager;

	public class addUserWindow extends JFrame implements ActionListener,KeyListener{
		private JFrame addUser = new JFrame();
		
		private JTextField firstName = new JTextField();
		private JTextField lastName = new JTextField();
		private JTextField email = new JTextField();
		
		private JButton add = new JButton();
		private JButton cancel = new JButton();
		
		private JLabel spacing = new JLabel();
		private JLabel spacing1 = new JLabel();
		private JLabel spacing2 = new JLabel();
		
		public addUserWindow(){
			//optional look and feel
			UIManager.LookAndFeelInfo[] lfi= UIManager.getInstalledLookAndFeels();
			    
			try
			{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			}
			catch (Exception e)
			{
			System.out.println("Can't set chosen look-and-feel!");
		    }

			addUser.setDefaultLookAndFeelDecorated(true);
			
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			addUser.setBounds(((int)(screenSize.getWidth()-400))/2,((int)(screenSize.getHeight()-400))/2,400,400);
			addUser.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			addUser.setLayout(new GridBagLayout());
			addUser.setTitle("Add User");
			
			addUser.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			
			//change the icon code
			String currentDirectory = System.getProperty("user.dir");
			try {
				Image image = ImageIO.read(new File(currentDirectory + "/src/jam/UserInterface/Icons/JAMicon.png"));
				addUser.setIconImage(image);
			}catch (Exception e){System.out.println("The file could not be read!!!!");}

			
			GridBagConstraints c = new GridBagConstraints();
			
			firstName.setPreferredSize(new Dimension(30,20));
			firstName.addKeyListener(this);
			lastName.setPreferredSize(new Dimension(30,20));
			lastName.addKeyListener(this);
			email.setPreferredSize(new Dimension(30,20));
			email.addKeyListener(this);
			
			JLabel fname = new JLabel();
			fname.setText("First Name: ");
			
			c.weightx = 1;
			c.fill = GridBagConstraints.BOTH;
			c.gridx = 0;
			c.gridy = 0;
			addUser.add(fname,c);
			
			spacing.setText(" ");
			
			c.gridx = 1;
			c.gridy = 0;
			addUser.add(spacing,c);
			
			c.gridx = 2;
			c.gridy = 0;
			firstName.setText(" ");
			firstName.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			addUser.add(firstName,c);
			
			JLabel lname = new JLabel();
			lname.setText("Last Name: ");
			c.gridx = 0;
			c.gridy = 1;
			addUser.add(lname,c);
			
			spacing1.setText(" ");
			
			c.gridx = 1;
			c.gridy = 1;
			addUser.add(spacing1,c);

			c.gridx = 2;
			c.gridy = 1;
			lastName.setText(" ");
			lastName.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			addUser.add(lastName,c);
			

			JLabel eAddress = new JLabel();
			eAddress.setText("Email Address: ");
			
			c.gridx = 0;
			c.gridy = 2;
			addUser.add(eAddress,c);
			
			spacing2.setText(" ");
			
			c.gridx = 1;
			c.gridy = 2;
			addUser.add(spacing2,c);
			
			c.gridx = 2;
			c.gridy = 2;
			email.setText(" ");
			email.setBorder(BorderFactory.createLineBorder(Color.BLACK));
			addUser.add(email,c);
			
			c.gridx = 0;
			c.gridy = 3;
			add.setText("add");
			add.addActionListener(this);
			addUser.add(add,c);

			JLabel spacing3 = new JLabel();
			spacing3.setText(" ");

			c.gridx = 1;
			c.gridy = 3;
			addUser.add(spacing3,c);

			c.gridx = 2;
			c.gridy = 3;
			cancel.setText("Cancel");
			cancel.addActionListener(this);
			addUser.add(cancel,c);
			
			addUser.setVisible(true);
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == add){
				if (firstName.getText().equals(" ")){
					spacing.setText("You need to put in a first name.");
				}
				else if(lastName.getText().equals(" ")){
					spacing1.setText("You need to put in a last name.");
				}
				else if(email.getText().equals(" ")){
					spacing2.setText("You need to put in an email address");
				}
				else {
					spacing.setText(" ");
					spacing1.setText(" ");
					spacing2.setText(" ");
				}
				System.out.println("Now it needs to add its contact list woo");
				addUser.dispose();
			}
			else if(e.getSource() == cancel){
				addUser.dispose();
			}
			
		}

		@Override
		public void keyPressed(KeyEvent e) {
			// TODO Auto-generated method stub
			if (e.getSource() == firstName && e.getKeyCode() == KeyEvent.VK_TAB){
				lastName.moveCaretPosition(0);
			}
			else if(e.getSource() == lastName && e.getKeyCode() == KeyEvent.VK_TAB){
				email.moveCaretPosition(0);
			}
			else if(e.getSource() == email && e.getKeyCode() == KeyEvent.VK_TAB){
				add.setFocusable(true);
			}
			
		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void keyTyped(KeyEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	}
